package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String begin = date + "-1";
        String end = date + "-31";
        Map<String,String> map = new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        //调用dao根据日期范围查询预约数据
        List<Map> list = orderSettingMapper.getOrderSettingByMonth(map);

        return list;
    }

    //批量导入预约设置数据
    @Override
    public void add(List<OrderSetting> data) {
        if (data!= null && data.size() >0){
            for (OrderSetting orderSetting : data) {
                long add = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
                if (add > 0)
                    //已经进行了预约设置，执行更新操作
                    orderSettingMapper.editNumByOrderDate(orderSetting);
                else
                    //没有进行预约设置执行更新操作
                    orderSettingMapper.add(orderSetting);
            }
        }
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        Date orderDate = orderSetting.getOrderDate();
        //根据日期查询是否已经进行了预约设置
        long count = orderSettingMapper.findCountByOrderDate(orderDate);
        if(count > 0){
            //当前日期已经进行了预约设置，需要执行更新操作
            orderSettingMapper.editNumByOrderDate(orderSetting);
        }else{
            //当前日期没有就那些预约设置，需要执行插入操作
            orderSettingMapper.add(orderSetting);
        }
    }
}
